package com.tiancheng.trade.order.controller.api;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.order.service.CreditOrderService;
import com.tiancheng.trade.order.vo.api.CreditOrderCancelVo;
import com.tiancheng.trade.order.vo.api.CreditOrderFinishVO;
import com.tiancheng.trade.order.vo.api.CreditOrderVo;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 *
 *信用订单
 * @author kellen
 * @date 2024/6/2
 */
@Slf4j
@RestController
@RequestMapping("/api/creditOrder")
@Validated
public class CreditOrderApiController {

    @Resource
    private CreditOrderService creditOrderService;

    /**
     * 查询预授权订单
     * @param alipayAuthNo
     * @param compatible 是否兼容老系统请求：Y-是，N—否，如果是则，会再次查询老系统订单
     * @return
     */
    @GetMapping(value = "/queryCreditOrder")
    public Result<CreditOrderVo> queryCreditOrderByAlipayAuthNo(@RequestParam("alipay_auth_no") String alipayAuthNo,
                                                                @RequestParam(required = false,value="compatible") String compatible) {
        return creditOrderService.queryCreditOrderByAlipayAuthNo(alipayAuthNo,compatible);
    }

    /**
     * 完成预授权订单流程
     * @param creditOrderFinishVO
     * @return
     */
    @PostMapping(value = "/finishCreditOrder",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result getOrderStr(@RequestBody @Valid CreditOrderFinishVO creditOrderFinishVO) {
        return creditOrderService.finishCreditOrder(creditOrderFinishVO);
    }

    /**
     * 取消预授权订单
     * @param orderCancelVo
     * @return
     */
    @PostMapping(value = "/cancelCreditOrder",consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result getOrderStr(@RequestBody @Valid CreditOrderCancelVo orderCancelVo) {
        return creditOrderService.cancel(orderCancelVo);
    }

    /**
     * 主动查询预授权回调状态
     * @param orderId
     * @param compatible 是否兼容老系统请求：Y-是，N—否，如果是则，会再次查询老系统订单
     * @return
     */
    @GetMapping(value = "/queryConfirmStatus")
    public Result queryConfirmStatusByOrderId(@RequestParam("order_id") String orderId,
                                              @RequestParam(required = false,value="compatible") String compatible) {
        CreditOrderVo creditOrder=creditOrderService.queryConfirmStatusByOrderId(orderId,compatible);
        if(creditOrder!=null){
            return Result.success(creditOrder);
        }
        return Result.success();
    }
}
